package com.netease.nie.strategy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.constants.CachePrefixConstants;
import com.netease.nie.constants.DisplayTypeConstants;
import com.netease.nie.dto.BannerDto;
import com.netease.nie.model.Banner;
import com.netease.nie.service.BannerService;

/**
 * 
 * @author wwbn3695
 * @createDate July 2rd,2016
 *
 */
@Service
public class BannerCustomConditionStrategy extends AbstractBannerStrategy {

	private static final String BANNER_LIST_PREFIX = CachePrefixConstants.BannerPrefix.BANNER_LIST_PREFIX;
	
	private static final String USER_BANNER_LIST_PREFIX = CachePrefixConstants.CustomPrefix.USER_BANNER_LIST_PREFIX;
	
	@Autowired
	private BannerService bannerService;
	
	@Autowired
	private Redis redis;
	
	public List<BannerDto> listBannerDtoByPosition(Long userId, int position) {
		if (userId <= 0)
			throw new BusinessException(BusinessConstants.INVALID_USER_ID.getCode(), "用户id不合法");
		String bannerListKey = BANNER_LIST_PREFIX + position;
		boolean exists = redis.exists(bannerListKey);
		List<Banner> bannerList = null;
		if (!exists) {
			bannerList = bannerService.queryAllBannerByPosition(position);
			if (CollectionUtils.isNotEmpty(bannerList))
				redis.rpush(bannerListKey, bannerList);
		} else
			bannerList = redis.lrange(bannerListKey, 0, -1, Banner.class);
		
		List<BannerDto> bannerDtoList = generateBannerDtoList(bannerList, DisplayTypeConstants.WAP.getCode());
		List<Integer> userBannerIdList = generateCustomBannerId(userId);
		filterBannerDtoList(bannerDtoList, userBannerIdList);
		return bannerDtoList;
	}
	
	private void filterBannerDtoList(List<BannerDto> bannerDtoList, List<Integer> userBannerIdList) {
		if (CollectionUtils.isNotEmpty(bannerDtoList)) {
			BannerDto bannerDto = null;
			if (userBannerIdList.isEmpty()) {
				Iterator<BannerDto> iterator = bannerDtoList.iterator();
				while (iterator.hasNext()) {
					bannerDto = iterator.next();
					if (bannerDto.getTags() == 0)
						iterator.remove();
				}
			} else {
				Iterator<BannerDto> iterator = bannerDtoList.iterator();
				while (iterator.hasNext()) {
					bannerDto = iterator.next();
					if (bannerDto.getTags() == 1) {
						if (!userBannerIdList.contains(bannerDto.getId()))
							iterator.remove();
					}
				}
			}
		}
	}
	
	private List<Integer> generateCustomBannerId(long userId) {
		String userBannerKey = USER_BANNER_LIST_PREFIX + userId;
		boolean exists = redis.exists(userBannerKey);
		if (exists) {
			List<Integer> userBannerList = new ArrayList<>();
			List<String> strList = redis.lrange(userBannerKey, 0, -1);
			if (strList != null && !strList.isEmpty()) {
				int strListSize = strList.size();
				String text = null;
				for (int i = 0; i < strListSize; i++) {
					text = strList.get(i);
					if (StringUtils.isNotBlank(text)) {
						int bannerId = Integer.valueOf(text);
						userBannerList.add(bannerId);
					}
				}
				return userBannerList;
			}
		}
		return Collections.emptyList();
	}

}
